package com.bjtu.transactionclient.service;

import com.bjtu.transactionclient.api.api.IndexService;
import com.bjtu.transactionclient.api.constant.Constans;
import com.bjtu.transactionclient.api.vo.request.SyncEntityRequest;
import com.bjtu.transactionclient.api.vo.request.UserIndexNoQueryTypeRequest;
import com.bjtu.transactionclient.base.exceptions.BaseException;
import com.bjtu.transactionclient.base.exceptions.ExceptionEnum;
import com.bjtu.transactionclient.base.result.Result;
import com.bjtu.transactionclient.dao.*;
import com.bjtu.transactionclient.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author caiyuli
 * @date 2020/4/23 3:43 下午
 */

@Service("IndexServiceImpl")
public class IndexServiceImpl implements IndexService {

    @Autowired
    private JournalEnglishDao journalEnglishDao;

    @Autowired
    private JournalChineseDao journalChineseDao;

    @Autowired
    private JournalPaperEnDao journalPaperEnDao;

    @Autowired
    private JournalPaperCnDao journalPaperCnDao;

    @Autowired
    private UserIndexDao userIndexDao;

    @Autowired
    private UserDao userDao;

    public String getRole(String indexNo)
    {
        return userDao.getRole(indexNo);
    }


    @Override
    public UserIndexNoTable queryUserIndexNo(String indexNo) {
        UserIndexNoTable userIndexNo = userIndexDao.queryUserIndexNoByIndex(indexNo);
        return userIndexNo;
    }

    @Override
    public JournalChinese queryJournalChineseByIndexNo(String index) {
        UserIndexNoTable userIndexNo = userIndexDao.queryUserIndexNoByIndex(index);
        verifyUserIndexNo(userIndexNo);
        if (!userIndexNo.equals(Constans.IndexCode.JOURNAL_CN)){
            throw new BaseException(ExceptionEnum.INDEX_TYPE_ERROR);
        }
        JournalChinese journalChinese = journalChineseDao.queryByIndexNo(index);
        if (journalChinese == null){
            throw new BaseException(ExceptionEnum.JOURNAL_CN_NULL);
        }
        return journalChinese;
    }

    @Override
    public JournalEnglish queryJournalEnglishByIndexNo(String indexNo) {
        UserIndexNoTable userIndexNo = userIndexDao.queryUserIndexNoByIndex(indexNo);
        verifyUserIndexNo(userIndexNo);
        if (!userIndexNo.equals(Constans.IndexCode.JOURNAL_EN)){
            throw new BaseException(ExceptionEnum.INDEX_TYPE_ERROR);
        }
        JournalEnglish  journalEnglish = journalEnglishDao.queryByIndexNo(indexNo);
        if (journalEnglish == null){
            throw new BaseException(ExceptionEnum.JOURNAL_EN_NULL);
        }
        return journalEnglish;
    }

    @Override
    public JournalPaperEnglish queryJournalPaperEnglishByIndexNo(String indexNo) {
        UserIndexNoTable userIndexNo = userIndexDao.queryUserIndexNoByIndex(indexNo);
        verifyUserIndexNo(userIndexNo);

        if (!userIndexNo.equals(Constans.IndexCode.JOURNAL_PAPER_EN)){
            throw new BaseException(ExceptionEnum.INDEX_TYPE_ERROR);
        }
        JournalPaperEnglish journalPaperEnglish = journalPaperEnDao.queryByIndexNo(indexNo);
        if (journalPaperEnglish == null){
            throw new BaseException(ExceptionEnum.JOURNAL_PAPER_EN_NULL);
        }

        return journalPaperEnglish;
    }

    @Override
    public JournalPaperChinese queryJournalPaperChineseByIndexNo(String indexNo) {
        UserIndexNoTable userIndexNo = userIndexDao.queryUserIndexNoByIndex(indexNo);
        verifyUserIndexNo(userIndexNo);

        if (!userIndexNo.equals(Constans.IndexCode.JOURNAL_PAPER_EN)){
            throw new BaseException(ExceptionEnum.INDEX_TYPE_ERROR);
        }
        JournalPaperChinese journalPaperChinese = journalPaperCnDao.queryByIndexNo(indexNo);
        if (journalPaperChinese == null){
            throw new BaseException(ExceptionEnum.JOURNAL_PAPER_CN_NULL);
        }
        return journalPaperChinese;

    }

    @Override
    public Result queryEntity(UserIndexNoQueryTypeRequest request) {

        if (request.getIndexNoCode().equals(Constans.IndexCode.JOURNAL_PAPER_CN)){
            return Result.success(queryJournalPaperChineseByIndexNo(request.getIndexNo()));
        }else if (request.getIndexNoCode().equals(Constans.IndexCode.JOURNAL_PAPER_EN)){
            return Result.success(queryJournalPaperEnglishByIndexNo(request.getIndexNo()));
        }else if (request.getIndexNoCode().equals(Constans.IndexCode.JOURNAL_CN)){
            return Result.success(queryJournalChineseByIndexNo(request.getIndexNo()));
        }else if (request.getIndexNoCode().equals(Constans.IndexCode.JOURNAL_EN)){
            return Result.success(queryJournalEnglishByIndexNo(request.getIndexNo()));
        }
        //以上是期刊及其期刊析出的文献
        else {
            throw new BaseException(ExceptionEnum.INDEX_TYPE_ERROR);
        }
    }

    private void verifyUserIndexNo(UserIndexNoTable userIndexNo){
        if (userIndexNo == null){
            throw new BaseException(ExceptionEnum.INDEX_BANK);
        }
    }


}
